package com.sxit.tdf.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxit.tdf.server.entity.Permission;
import com.sxit.tdf.server.entity.bo.InsertPermissionBo;
import com.sxit.tdf.server.entity.bo.PermissionInfoBo;
import com.sxit.tdf.server.entity.bo.RolePermissionBo;
import com.sxit.tdf.server.mapper.PermissionMapper;
import com.sxit.tdf.server.service.IPermissionService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 服务实现类
 *
 * @author 橙子高
 * @since 2022-02-11
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission>
    implements IPermissionService {

  @Override
  public List<PermissionInfoBo> getPermissionList() {
    return baseMapper.getPermissionList();
  }

  @Override
  public HashMap<String, List<RolePermissionBo>> getRolePermissionList(int id) {

    HashMap<String, List<RolePermissionBo>> roleList = new HashMap<>();

    List<RolePermissionBo> rolePermissionList = baseMapper.getRolePermissionList(id);
    rolePermissionList.forEach(
        item -> {
          List<RolePermissionBo> rolePermissionBos = roleList.get(item.getModuleCode());
          if (rolePermissionBos == null) {
            List<RolePermissionBo> newRolePermissionList = new ArrayList<>();
            newRolePermissionList.add(item);
            roleList.put(item.getModuleCode(), newRolePermissionList);
          } else {
            rolePermissionBos.add(item);
            roleList.put(item.getModuleCode(), rolePermissionBos);
          }
        });

    return roleList;
  }

  @Override
  public HashMap<String, List<InsertPermissionBo>> getInsertPermissionList() {

    HashMap<String, List<InsertPermissionBo>> result = new HashMap<>();

    baseMapper
        .getInsertPermissionList()
        .forEach(
            item -> {
              List<InsertPermissionBo> insertPermissionBos = result.get(item.getModuleCode());
              if (insertPermissionBos == null) {
                ArrayList<InsertPermissionBo> newInsertPermissionBo = new ArrayList<>();
                newInsertPermissionBo.add(item);
                result.put(item.getModuleCode(), newInsertPermissionBo);
              } else {
                insertPermissionBos.add(item);
                result.put(item.getModuleCode(), insertPermissionBos);
              }
            });

    return result;
  }
}
